package com.hunttown.mes.impl;

import com.hunttown.common.domain.Page;
import com.hunttown.common.domain.Query;
import com.hunttown.mes.common.utils.StringUtils;
import com.hunttown.mes.rpc.api.MesProduceWorkmanshipRpcService;
import com.hunttown.mes.rpc.domain.MesProduceWorkmanshipDTO;
import com.hunttown.mes.rpc.domain.query.MesProduceWorkmanshipDTOQuery;
import com.hunttown.mes.service.MesProduceWorkmanshipService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.hunttown.mes.convert.MesProduceWorkmanshipConverts.convert;

/**
 * created by wangjunfu on 2022-03-30 15:32:14
 */
@Service
public class MesProduceWorkmanshipRpcServiceImpl implements MesProduceWorkmanshipRpcService {

    private final Logger logger = LoggerFactory.getLogger(MesProduceWorkmanshipRpcServiceImpl.class);

    private final MesProduceWorkmanshipService mesProduceWorkmanshipService;

    @Autowired
    public MesProduceWorkmanshipRpcServiceImpl(MesProduceWorkmanshipService mesProduceWorkmanshipService) {
        this.mesProduceWorkmanshipService = mesProduceWorkmanshipService;
    }

    @Override
    public MesProduceWorkmanshipDTO insert(MesProduceWorkmanshipDTO mesProduceWorkmanshipDTO) {
        try {
            return convert(mesProduceWorkmanshipService.insert(convert(mesProduceWorkmanshipDTO)));
        } catch (Exception e) {
            logger.error("MesProduceWorkmanship create failed.", e);
            throw new RuntimeException("MesProduceWorkmanship create failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoById(MesProduceWorkmanshipDTO mesProduceWorkmanshipDTO) {
        try {
            return mesProduceWorkmanshipService.updateInfoById(convert(mesProduceWorkmanshipDTO));
        } catch (Exception e) {
            logger.error("MesProduceWorkmanship update by id failed.", e);
            throw new RuntimeException("MesProduceWorkmanship update by id failed, Please order your code.", e);
        }
    }

    @Override
    public Boolean updateInfoByQuery(MesProduceWorkmanshipDTOQuery mesProduceWorkmanshipDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProduceWorkmanshipDTOQuery.getW_id() != null && mesProduceWorkmanshipDTOQuery.getW_id() > 0) {
                query.put("w_id", mesProduceWorkmanshipDTOQuery.getW_id());
            }

            if (mesProduceWorkmanshipDTOQuery.getIds() != null && !mesProduceWorkmanshipDTOQuery.getIds().equals("")) {
                query.put("ids", mesProduceWorkmanshipDTOQuery.getIds());
            }

            if (mesProduceWorkmanshipDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProduceWorkmanshipDTOQuery.getStopFlag());
            }

            if (mesProduceWorkmanshipDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProduceWorkmanshipDTOQuery.getDeleteFlag());
            }

            return mesProduceWorkmanshipService.updateInfoByQuery(query);
        } catch (Exception e) {
            logger.error("MesProduceWorkmanship update by query failed.", e);
            throw new RuntimeException("MesProduceWorkmanship update by query failed, Please order your code.", e);
        }
    }

    @Override
    public MesProduceWorkmanshipDTO selectById(Integer id) {
        try {
            return convert(mesProduceWorkmanshipService.selectById(id));
        } catch (Exception e) {
            logger.error("MesProduceWorkmanship select by id failed.", e);
            throw new RuntimeException("MesProduceWorkmanship select by id failed, Please order your code.", e);
        }
    }

    @Override
    public MesProduceWorkmanshipDTO selectObjByQuery(MesProduceWorkmanshipDTOQuery mesProduceWorkmanshipDTOQuery) {
        try {
            Query query = Query.page(1, 5);

            if (mesProduceWorkmanshipDTOQuery.getStopFlag() != null) {
                query.put("stopFlag", mesProduceWorkmanshipDTOQuery.getStopFlag());
            }

            if (mesProduceWorkmanshipDTOQuery.getDeleteFlag() != null) {
                query.put("deleteFlag", mesProduceWorkmanshipDTOQuery.getDeleteFlag());
            }

            if (StringUtils.isNotBlank(mesProduceWorkmanshipDTOQuery.getOrderBy())) {
                query.put("orderBy", mesProduceWorkmanshipDTOQuery.getOrderBy());
            }

            return convert(mesProduceWorkmanshipService.selectObjByQuery(query));
        } catch (Exception e) {
            logger.error("MesProduceWorkmanship select obj by query failed.", e);
            throw new RuntimeException("MesProduceWorkmanship select obj by query failed, Please order your code.", e);
        }
    }

    @Override
    public Page<MesProduceWorkmanshipDTO> getListForPage(MesProduceWorkmanshipDTOQuery mesProduceWorkmanshipDTOQuery) {
        Query query = Query.page(mesProduceWorkmanshipDTOQuery.getPn(), mesProduceWorkmanshipDTOQuery.getPageSize());

        if (mesProduceWorkmanshipDTOQuery.getId() != null && mesProduceWorkmanshipDTOQuery.getId() > 0) {
            query.put("id", mesProduceWorkmanshipDTOQuery.getId());
        }

        if (mesProduceWorkmanshipDTOQuery.getIds() != null && !mesProduceWorkmanshipDTOQuery.getIds().equals("")) {
            query.put("ids", mesProduceWorkmanshipDTOQuery.getIds());
        }

        if (StringUtils.isNotBlank(mesProduceWorkmanshipDTOQuery.getLikeName())) {
            query.put("likeName", mesProduceWorkmanshipDTOQuery.getLikeName());
        }

        if (StringUtils.isNotBlank(mesProduceWorkmanshipDTOQuery.getShowDate())) {
            query.put("showDate", mesProduceWorkmanshipDTOQuery.getShowDate());
        }

        if (StringUtils.isNotBlank(mesProduceWorkmanshipDTOQuery.getStartDate())) {
            query.put("startDate", mesProduceWorkmanshipDTOQuery.getStartDate());
        }

        if (StringUtils.isNotBlank(mesProduceWorkmanshipDTOQuery.getEndDate())) {
            query.put("endDate", mesProduceWorkmanshipDTOQuery.getEndDate());
        }

        if (mesProduceWorkmanshipDTOQuery.getStopFlag() != null) {
            query.put("stopFlag", mesProduceWorkmanshipDTOQuery.getStopFlag());
        }

        if (mesProduceWorkmanshipDTOQuery.getDeleteFlag() != null) {
            query.put("deleteFlag", mesProduceWorkmanshipDTOQuery.getDeleteFlag());
        }

        if (StringUtils.isNotBlank(mesProduceWorkmanshipDTOQuery.getOrderBy())) {
            query.put("orderBy", mesProduceWorkmanshipDTOQuery.getOrderBy());
        }

        List<MesProduceWorkmanshipDTO> list = convert(mesProduceWorkmanshipService.selectByQuery(query));
        Page<MesProduceWorkmanshipDTO> page = Page.from(mesProduceWorkmanshipDTOQuery.getPn(), mesProduceWorkmanshipDTOQuery.getPageSize(), list);
        page.setTotalItems(mesProduceWorkmanshipService.countByQuery(query));

        return page;
    }

    @Override
    public Boolean deleteById(Integer id) {
        try {
            return mesProduceWorkmanshipService.deleteById(id);
        } catch (Exception e) {
            logger.error("MesProduceWorkmanship delete failed.", e);
            throw new RuntimeException("MesProduceWorkmanship delete failed, Please order your code.", e);
        }
    }
}